package edu.hawaii.myisern.action;

import static org.junit.Assert.assertEquals;
import org.junit.Test;
import com.meterware.httpunit.WebConversation;
import com.meterware.httpunit.WebForm;
import com.meterware.httpunit.WebLink;
import com.meterware.httpunit.WebRequest;
import com.meterware.httpunit.WebResponse;


/**
 * Tests the collaboration action bean.
 * 
 * @author Ed Kim
 * @author Jon Lao
 * @author JianFei Liao
 * @author Jared Sunouchi
 */
public class TestCollaborationActionBean {
  
  /** Get the test host. */
  private String testHost = System.getProperty("test.host");
  /** A string representing the homepage title */
  private static final String HOMEPAGE_TITLE = "MyISERN 2.0 - Home";
  /** A string representing the login page title */
  private static final String LOGIN_TITLE = "MyISERN 2.0 - Login";
  /** A string represent the username attribute */
  private static final String USERNAME_FIELD = "username";
  /** A string represent the password attribute */
  private static final String PASSWORD_FIELD = "password";
  /** A string representing the user name */
  private static final String USERNAME = "username";
  /** A string representing the password */
  private static final String PASSWORD = "password";
  /** A string representing the collaboration form */
  private static final String COLLABORATION_FORM = "CollaborationForm";
  /** A string to represent the name field */
  private static final String NAME = "name";
  private static final String YEARS = "years";
  
  /**
   * Tests the add collaborations feature.
   * 
   * @throws Exception If error occur.
   */
  @Test
  public void testAddCollaboration() throws Exception {    
    WebConversation conversation = new WebConversation();
    
    // retrieves the index.jsp page and check the title
    String url = this.testHost + "myisern";
    WebResponse response = conversation.getResponse(url);
    assertEquals("Clicking link without login", LOGIN_TITLE, response.getTitle());
    
    WebForm loginForm = response.getFormWithID("LoginForm");
    WebRequest loginRequest = loginForm.getRequest();
    
    loginRequest.setParameter(USERNAME_FIELD, USERNAME);
    loginRequest.setParameter(PASSWORD_FIELD, PASSWORD);
    response = conversation.getResponse(loginRequest);
    assertEquals("Logged in", HOMEPAGE_TITLE, response.getTitle());
    
    WebLink link = response.getLinkWithID("addCollaboration");
    response = link.click();
    assertEquals("Adding collaboration", "MyISERN 2.0 - Add Collaboration", response.getTitle());
    
    WebForm addCollaborationForm = response.getFormWithID(COLLABORATION_FORM);
    WebRequest addCollaborationRequest = addCollaborationForm.getRequest("reset");
    addCollaborationRequest.setParameter(NAME, "Testing Clear");
    response = conversation.getResponse(addCollaborationRequest);
    
    addCollaborationForm = response.getFormWithID(COLLABORATION_FORM);
    addCollaborationRequest = addCollaborationForm.getRequest("add");
    response = conversation.getResponse(addCollaborationRequest);
    
    addCollaborationForm = response.getFormWithID(COLLABORATION_FORM);
    addCollaborationRequest =  addCollaborationForm.getRequest("add");
    
    addCollaborationRequest.setParameter(NAME, "Test Name");
    addCollaborationRequest.setParameter("collaboratingOrganizations", 
        "Test Collaborating Organization 1, Test Collaborating Organization 2");
    addCollaborationRequest.setParameter("collaborationTypes", 
        "Test Collaboration Type 1, Test Collaboration Types 2");
    addCollaborationRequest.setParameter(YEARS, "2006, 2007");
    addCollaborationRequest.setParameter("outcomeTypes", 
        "Test Outcome Type 1, Test Outcome Type 2");
    addCollaborationRequest.setParameter("description", "Test Description");
    
    response = conversation.getResponse(addCollaborationRequest);
    
    link = response.getLinkWithID("addCollaboration");
    response = link.click();
    assertEquals("Adding collaboration", "MyISERN 2.0 - Add Collaboration", response.getTitle());
    
    addCollaborationForm = response.getFormWithID(COLLABORATION_FORM);
    addCollaborationRequest = addCollaborationForm.getRequest("add");
    
    addCollaborationRequest.setParameter(NAME, "Test Name");
    response = conversation.getResponse(addCollaborationRequest);
    
    addCollaborationRequest.setParameter("collaboratingOrganizations", 
        "Test Collaborating Organization");
    response = conversation.getResponse(addCollaborationRequest);
    
    addCollaborationRequest.setParameter("collaboratingOrganizations", 
        "Test Collaborating Organization 1, Test Collaborating Organization 2");
    response = conversation.getResponse(addCollaborationRequest);
    
    addCollaborationRequest.setParameter("collaborationTypes", 
        "Test Collaboration Type 1, Test Collaboration Types 2");
    response = conversation.getResponse(addCollaborationRequest);
    
    addCollaborationRequest.setParameter(YEARS, "Not valid years");
    response = conversation.getResponse(addCollaborationRequest);
    
    addCollaborationRequest.setParameter(YEARS, "3000");
    response = conversation.getResponse(addCollaborationRequest);
    
    addCollaborationRequest.setParameter(YEARS, "2006, 2007");
    response = conversation.getResponse(addCollaborationRequest);
    
    addCollaborationRequest.setParameter("outcomeTypes", 
        "Test Outcome Type 1, Test Outcome Type 2");
    addCollaborationRequest.setParameter("description", "Test Description");
    
    response = conversation.getResponse(addCollaborationRequest);
    assertEquals("Went to edit page", "MyISERN 2.0 - Edit Collaboration", response.getTitle());
    
    link = response.getLinkWithID("addCollaboration");
    response = link.click();
    assertEquals("Adding collaboration", "MyISERN 2.0 - Add Collaboration", response.getTitle());
    
    addCollaborationForm = response.getFormWithID(COLLABORATION_FORM);
    addCollaborationRequest = addCollaborationForm.getRequest("cancel");
    response = conversation.getResponse(addCollaborationRequest);
    
    link = response.getLinkWithID("logout");
    link.click();
    response = conversation.getCurrentPage();
    assertEquals("Logged out", LOGIN_TITLE, response.getTitle());
  }
  
  /**
   * Tests the edit collaboration feature.
   *
   * @throws Exception If problems occur.
   */
  @Test
  public void testEditCollaborationActionBean() throws Exception {
    WebConversation conversation = new WebConversation();
    
    // retrieves the index.jsp page and check the title
    String url = this.testHost + "myisern";
    WebResponse response = conversation.getResponse(url);
    assertEquals("Clicking link without login", LOGIN_TITLE, response.getTitle());
    
    WebForm loginForm = response.getFormWithID("LoginForm");
    WebRequest loginRequest = loginForm.getRequest();
    
    loginRequest.setParameter(USERNAME_FIELD, USERNAME);
    loginRequest.setParameter(PASSWORD_FIELD, PASSWORD);
    response = conversation.getResponse(loginRequest);
    assertEquals("Logged in", HOMEPAGE_TITLE, response.getTitle());
    
    WebLink link = response.getLinkWithID("editCollaboration");
    response = link.click();
    assertEquals("Checking edit collaboration page retrieval", "MyISERN 2.0 - Edit Collaboration",
        response.getTitle());
    
    WebForm editCollaborationForm = response.getFormWithID(COLLABORATION_FORM);
    WebRequest editCollaborationRequest = editCollaborationForm.getRequest();
    
    editCollaborationRequest.setParameter(NAME, "Test Name");
    
    response = conversation.getResponse(editCollaborationRequest);
    
    editCollaborationForm = response.getFormWithID(COLLABORATION_FORM);
    editCollaborationRequest = editCollaborationForm.getRequest("edit");
    
    editCollaborationRequest.setParameter("description", "Test Description Edited");
    
    response = conversation.getResponse(editCollaborationRequest);
    
    editCollaborationForm = response.getFormWithID(COLLABORATION_FORM);
    editCollaborationRequest = editCollaborationForm.getRequest("remove");
    response = conversation.getResponse(editCollaborationRequest);
    
    link = response.getLinkWithID("logout");
    link.click();
    response = conversation.getCurrentPage();
    assertEquals("Logged out", LOGIN_TITLE, response.getTitle());
  }
}
